package com.hitgo.soccer.log;

import java.io.File;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class FBLogger
{
  private static final Logger logger = Logger.getLogger("com.hitgo.socialfootball.log.FBLogger");
  private static boolean inited = false;
  private static int LOG_MAX_SIZE = 10000000;
  private static int LOG_MAX_FILES = 10;
  private static Level FILE_LOGGIN_LEVEL = Level.WARNING;
  private static final Level CONSOLE_LOGGIN_LEVEL = Level.WARNING;
  private static final String LOGGING_FOLDER = "logs/bluebox/";

  public static Logger getLogger()
  {
    if (!inited)
    {
      initLogger();
      inited = true;
    }

    return logger;
  }

  private static void initLogger()
  {
    checkLoggingFolder();

    Logger rootLogger = Logger.getLogger("");
    Handler[] handlers = rootLogger.getHandlers();
    if ((handlers[0] instanceof ConsoleHandler)) {
      rootLogger.removeHandler(handlers[0]);
    }

    ConsoleHandler ch = null;
    FileHandler fh = null;
    try
    {
      ch = new ConsoleHandler();

      String logPath = "d:/logs/bluebox/";
      fh = new FileHandler(logPath + "bbox_log_%g.txt", LOG_MAX_SIZE, LOG_MAX_FILES);
    }
    catch (Exception e)
    {
      e.printStackTrace();
    }

    ch.setLevel(CONSOLE_LOGGIN_LEVEL);
    fh.setLevel(FILE_LOGGIN_LEVEL);

    logger.addHandler(ch);
    logger.addHandler(fh);

    fh.setFormatter(new FBLogFormatter());
    ch.setFormatter(new FBLogFormatter());
  }

  private static void checkLoggingFolder()
  {
    File logFolder = new File("logs/bluebox/");

    if (!logFolder.isDirectory())
      logFolder.mkdir();
  }
}